#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: add_snippet.py
# @time: 2021/8/6 13:51
# @desc:
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser

snippet = Snippet(code='foo = "bar"\n', owner_id=1)
snippet.save()

snippet = Snippet(code='print "hello, world"\n', owner_id=1)
snippet.save()

serializer = SnippetSerializer(snippet)
print(serializer.data)

content = JSONRenderer().render(serializer.data)
print(content)

import io

stream = io.BytesIO(content)
data = JSONParser().parse(stream)

serializer = SnippetSerializer(data=data)
print(serializer.is_valid())
print(serializer.validated_data)
serializer.save()

serializer = SnippetSerializer(Snippet.objects.all(), many=True)
print(serializer.data)

serializer = SnippetSerializer()
print(repr(serializer))
